package com.example.camerafacedetect.faceframe2;

import android.graphics.Bitmap;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.mlkit.vision.common.InputImage;
import com.google.mlkit.vision.face.Face;
import com.google.mlkit.vision.face.FaceDetection;
import com.google.mlkit.vision.face.FaceDetector;
import com.google.mlkit.vision.face.FaceDetectorOptions;

import java.util.List;

/**
 * Created by yubo on 2015/9/5.
 * 人脸检测工具类
 */
public class FaceDetectorUtils {

    private static FaceDetector faceDetector;

    private FaceDetectorUtils(){
    }

    public interface Callback{
        void onFaceDetected(List<Face> faces, Bitmap bitmap);
        void onFaceNotDetected(Bitmap bitmap);
    }

    /**
     * 检测bitmap中的人脸，在callback中返回人脸数据
     * @param bitmap
     * @param callback
     */
    public static void detectFace(Bitmap bitmap,FaceDetectorUtils.Callback callback){
        try {
            InputImage image = InputImage.fromBitmap(bitmap, 0);
            FaceDetectorOptions realTimeOpts =
                    new FaceDetectorOptions.Builder()
                            .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)
                            .build();
            FaceDetector detector = FaceDetection.getClient(realTimeOpts);
            Task<List<Face>> result =
                    detector.process(image)
                            .addOnSuccessListener(
                                    new OnSuccessListener<List<com.google.mlkit.vision.face.Face>>() {
                                        @Override
                                        public void onSuccess(List<com.google.mlkit.vision.face.Face> faces) {
                                            Log.e("++++++++++++", "find face number: " + faces.size());
                                            // Task completed successfully
                                            // [START_EXCLUDE]
                                            // [START get_face_info]
//                                            for (com.google.mlkit.vision.face.Face face : faces) {
//                                                Rect bounds = face.getBoundingBox();
//
//                                                float rotY = face.getHeadEulerAngleY();  // Head is rotated to the right rotY degrees
//                                                float rotZ = face.getHeadEulerAngleZ();  // Head is tilted sideways rotZ degrees
//
//                                                // If landmark detection was enabled (mouth, ears, eyes, cheeks, and
//                                                // nose available):
//                                                FaceLandmark leftEar = face.getLandmark(FaceLandmark.LEFT_EAR);
//                                                if (leftEar != null) {
//                                                    PointF leftEarPos = leftEar.getPosition();
//                                                }
//
//                                                // If classification was enabled:
//                                                if (face.getSmilingProbability() != null) {
//                                                    float smileProb = face.getSmilingProbability();
//                                                }
//                                                if (face.getRightEyeOpenProbability() != null) {
//                                                    float rightEyeOpenProb = face.getRightEyeOpenProbability();
//                                                }
//
//                                                // If face tracking was enabled:
//                                                if (face.getTrackingId() != null) {
//                                                    int id = face.getTrackingId();
//                                                }

//                                            }
                                            callback.onFaceDetected(faces, bitmap);
                                            // [END get_face_info]
                                            // [END_EXCLUDE]
                                        }
                                    })
                            .addOnFailureListener(
                                    new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.e("+++++++++", "face detect failture");
                                        }
                                    });

//            if(faceNum > 0) {
//                Log.e("++++++++++++", "find face number: " + faceNum);
//                if(callback != null) {
//                    //callback.onFaceDetected(faces, bitmap);
//                }
//            }else{
//                if(callback != null) {
//                    callback.onFaceNotDetected(bitmap);
//                    bitmap.recycle();
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
